<template>
  <div id="users">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="40">
        <el-col :span="9">
          <el-input v-model="queryInfo.query" clearable @clear="getUsersList()" placeholder="请输入内容">
            <el-button @click="getUsersList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addUsersDialog">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table border :data="usersList" stripe style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态" width="60">
          <template slot-scope="scope">
            <el-switch
              @change="changState(scope.row)"
              v-model="scope.row.mg_state"
              active-color="#66B1FF"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="editUsersDialog(scope.row.id)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              @click="openDeleteUsers(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
            ></el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog
      title="添加用户"
      @close="addUsersClose"
      :visible.sync="AddUsersdialogVisible"
      width="50%"
    >
      <el-form :model="addfrom" :rules="AddUsersRules" ref="AddUsersRef" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addfrom.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addfrom.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addfrom.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="addfrom.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AddUsersdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddUsers">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改用户" @close="addUsersClose" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editFrom" :rules="AddUsersRules" ref="AddUsersRef" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="editFrom.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editFrom.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editFrom.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="EditUsers">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2
      },
      AddUsersRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 51 个字符", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请输入电话", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "长度在 11个字符",
            trigger: "blur"
          }
        ]
      },
      usersList: [],
      total: 0,
      AddUsersdialogVisible: false,
      addfrom: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      editDialogVisible: false,
      editFrom: {
        username: "",
        editFrom: "",
        mobile: ""
      }
    };
  },
  created() {
    this.getUsersList();
  },
  methods: {
    async getUsersList() {
      const { data: res } = await this.$http.get("users", {
        params: this.queryInfo
      });
      this.usersList = res.data.users;
      this.total = res.data.total;
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getUsersList();
    },
    handleCurrentChange(newsize) {
      this.queryInfo.pagenum = newsize;
      this.getUsersList();
    },
    async changState(usersInfo) {
      const { data: res } = await this.$http.put(
        `users/${usersInfo.id}/state/${usersInfo.mg_state}`
      );
    },
    addUsersDialog() {
      this.AddUsersdialogVisible = true;
    },
    addUsersClose() {
      this.$refs.AddUsersRef.resetFields();
    },
    AddUsers() {
      console.log(this.addfrom);

      this.$refs.AddUsersRef.validate(async val => {
        if (val) {
          const { data: res } = await this.$http.post("users", this.addfrom);
          console.log(res);
          this.getUsersList();
        }
        this.AddUsersdialogVisible = false;
      });
    },
    async editUsersDialog(id) {
      const { data: res } = await this.$http.get("users/" + id);
      this.editFrom = res.data;
      this.editDialogVisible = true;
    },
    EditUsers() {
      this.$refs.AddUsersRef.validate(async val => {
        if (val) {
          const { data: res } = await this.$http.put(
            "users/" + this.editFrom.id,
            {
              email: this.editFrom.email,
              mobile: this.editFrom.mobile
            }
          );
          this.$message.success("更新成功！");
          this.editDialogVisible = false;
          this.getUsersList();
        }
      });
    },
    openDeleteUsers(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("users/" + id);
          this.getUsersList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
</style>